package com.bagua.sbbcshop.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author x.hou SHA-1 加密
 */
public class EncodeSHA1 {
	
	private static final Logger logger = LoggerFactory.getLogger(EncodeSHA1.class);

	public static String getSHA1(String inStr) {
		MessageDigest md = null;
		String outStr = null;
		try {
			md = MessageDigest.getInstance("SHA-1");// 选择SHA-1
			byte[] digest = md.digest(inStr.getBytes());// 返回的是byet[]，要转化为String存储比较方便
			String tempStr = "";
			for (int i = 1; i < digest.length; i++) {
				tempStr = (Integer.toHexString(digest[i] & 0xff));
				if (tempStr.length() == 1) {
					outStr = outStr + "0" + tempStr;
				} else {
					outStr = outStr + tempStr;
				}
			}
		} catch (NoSuchAlgorithmException nsae) {
			logger.error(nsae.getMessage(), nsae);
		}
		return outStr.toLowerCase();
	}

}
